package test002;

//import java.io.PrintStream;
import java.io.StringWriter;
import java.io.Writer;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.data.contacts.ContactGroupFeed;
import com.google.gdata.data.extensions.ExtendedProperty;
import com.google.appengine.api.users.User;
//import com.google.gdata.util.AuthenticationException;
//import com.google.gdata.util.ServiceException;

//import java.io.IOException;
//import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class MyContacts {

	private ContactsService myService;
	private User myUser;

	private MyContacts(){};
	
	public MyContacts(User myUser, String sessionToken) {
		this();
		this.myUser = myUser;
		myService = new ContactsService("My Application");
		myService.setAuthSubToken(sessionToken);
	}

	public String getMyGroups() {
		Writer myWriter = new StringWriter();
		try {
			// Request the feed
			URL metafeedUrl = new URL("https://www.google.com/m8/feeds/groups/"
					+ myUser.getNickname() + "@gmail.com/full");
			// URL metafeedUrl = new
			// URL("https://www.google.com/m8/feeds/contacts/"+
			// myUser.getNickname() +"@gmail.com/base");
			ContactGroupFeed resultFeed = myService.getFeed(metafeedUrl,
					ContactGroupFeed.class);
			// Print the results
			myWriter.write(resultFeed.getTitle().getPlainText());

			for (int i = 0; i < resultFeed.getEntries().size(); i++) {
				myWriter.write("</p>");
				ContactGroupEntry groupEntry = resultFeed.getEntries().get(i);
				myWriter.write("Id: " + groupEntry.getId());
				myWriter.write("Group Name: "
						+ groupEntry.getTitle().getPlainText());
				myWriter.write("Last Updated: " + groupEntry.getUpdated());
				myWriter.write("Extended Properties:");
				for (ExtendedProperty property : groupEntry
						.getExtendedProperties()) {
					if (property.getValue() != null) {
						myWriter.write("  " + property.getName() + "(value) = "
								+ property.getValue());
					} else if (property.getXmlBlob() != null) {
						myWriter.write("  " + property.getName()
								+ "(xmlBlob) = "
								+ property.getXmlBlob().getBlob());
					}
				}
				myWriter.write("Self Link: "
						+ groupEntry.getSelfLink().getHref());
				if (!groupEntry.hasSystemGroup()) {
					// System groups do not have an edit link
					myWriter.write("Edit Link: "
							+ groupEntry.getEditLink().getHref());
					myWriter.write("ETag: " + groupEntry.getEtag());
				}
				if (groupEntry.hasSystemGroup()) {
					myWriter.write("System Group Id: "
							+ groupEntry.getSystemGroup().getId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return myWriter.toString();
	}

	/**
	 * This is a test template
	 */

	// public String getMyContacts(String user, String paswd) {
	public String getMyContacts(User myUser, String sessionToken) {
		Writer myWriter = new StringWriter();

		try {

			// Create a new Contacts service

			// ### new auth end

			// Get a list of all entries
			URL metafeedUrl = new URL(
					"http://www.google.com/m8/feeds/contacts/"
							// + myUser.getNickname() + "@gmail.com/base");
							+ myUser.getNickname() + "@gmail.com/full/groupId");
			myWriter.write("Getting Contacts entries...\n");
			ContactFeed resultFeed = myService.getFeed(metafeedUrl,
					ContactFeed.class);
			List<ContactEntry> entries = resultFeed.getEntries();
			for (int i = 0; i < entries.size(); i++) {
				ContactEntry entry = entries.get(i);
				myWriter.write("\t" + entry.getTitle().getPlainText());
			}
			myWriter.write("\nTotal Entries: " + entries.size());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return myWriter.toString();
	}
}
